package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> findCateogryByParentId(Long pid) {
//        select * from tb_category where parent_id=??
        Category category = new Category();
        category.setParentId(pid); //设置属性作为条件查询
        List<Category> categoryList = categoryMapper.select(category);
        List<CategoryDTO> categoryDTOList = BeanHelper.copyWithCollection(categoryList, CategoryDTO.class);
        if(CollectionUtils.isEmpty(categoryDTOList)){  //判断分类数据是否为空
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categoryDTOList;

    }

    /**
     * 根据品牌id查出分类数据
     *select c.* from tb_category  c,tb_category_brand cb where c.id=cb.category_id and cb.brand_id=1528
     * @param id
     * @return
     */
    public List<CategoryDTO> findCateogryByBrandId(Long id) {
        List<Category>  categoryList = categoryMapper.findCateogryByBrandId(id);
        if(CollectionUtils.isEmpty(categoryList)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categoryList,CategoryDTO.class);

    }

    public List<CategoryDTO> findCateogryByCids(List<Long> ids) {
        List<Category>  categoryList = categoryMapper.selectByIdList(ids);
        if(CollectionUtils.isEmpty(categoryList)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categoryList,CategoryDTO.class);
    }
}
